<?php

namespace App\Module\Api\Controllers;

use App\Exceptions\Customize\ParamsException;
use App\Module\Api\Controllers\Request\RegisterSubmitRequest;
use App\System\Cis\Repositories\CisUserRepository;
use Illuminate\Http\Request;


class RegisterSubmitController extends Controller
{
    public function __construct()
    {
        $this->query = new RegisterSubmitRequest();
        parent::__construct();
    }

    public function index()
    {
        $action = $this->query->action;
            $user = CisUserRepository::getInstance();
            $mobile = $user->hasMobile($this->query->mobile);
            if ($mobile){
                throw new ParamsException([
                    'error_code' => '1000',
                    'message'    => '手机号已存在',
                ]);
            }
            $userinfo=array();
            if ($action!=1){
//                分享注册 根据用户id查询
                $user_id = $this->query->id;
                $shareUser = $user->getUser($user_id);
                if (!$shareUser){
                    throw new ParamsException([
                        'error_code' => '1000',
                        'message'    => '分享者不合法',
                    ]);
                }
                $userinfo['user_id'] = $user_id;
            }
            $userinfo['name'] = $this->query->nickname;
            $userinfo['mobile']=$this->query->mobile;
            $userinfo['password']=$this->query->password;
            $userid = $user->CreateUser($userinfo);
           if (!$userid){
               throw new ParamsException([
                   'error_code' => '1000',
                   'message'    => '注册失败！',
               ]);
           }
           return apiResponse(['d'=>'注册成功']);


    }
}
